<?php

namespace app\admin\controller\fssy;

use app\common\controller\Backend;
use app\common\MyLibrary\Utils; // 引入工具类 application\admin\command\MyLibrary\Utils.php
use think\Log;
use think\Request;

/**
 * 各科室指标统计表 - 生成控制器使用
 *
 * @icon fa fa-circle-o
 */
class Departmetricscount extends Backend
{

    /**
     * Departmetricscount模型对象
     * @var \app\admin\model\fssy\Departmetricscount
     */
    protected $model = null;

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = ['getDepartTricsCountData'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\fssy\Departmetricscount;
    }

    public function index()
    {
        $this->model = new \app\admin\model\dicts\Department;
        // 获取 id=0 的 department 值
        $departmentList = $this->model->field('department')->select();
        array_unshift($departmentList, ['department' => '全部科室']);
        // 将默认值传递给视图
        $this->assign('departmentList', $departmentList);
        Log::record('departmentList = ' . json_encode($departmentList));

        // 获取数据库总更新时间的最大值和最小值
        $this->model = new \app\admin\model\fssy\Departmentmetrics();
        // 获取最小的 updatetime 数据
        $min_updatetime_data = $this->model->order('updatetime', 'asc')->limit(1)->find();
        // 获取最大的 updatetime 数据
        $max_updatetime_data = $this->model->order('updatetime', 'desc')->limit(1)->find();
        // 时间戳转换成时间 返回2022-05-10 格式
        $min_updatetime = date('Y-m', $min_updatetime_data['updatetime']);
        // 时间戳转换成时间 返回2024-12-07格式
        $max_updatetime = date('Y-m', $max_updatetime_data['updatetime']);
        // 转换成 年 季度 "2024年 四季度"
        $monthList = Utils::generateMonthlyRange($min_updatetime, $max_updatetime);
        array_unshift($monthList, ['month' => '']);
        $this->assign('monthList', $monthList);

        return $this->fetch();
    }

    public function getDepartTricsCountData(Request $request)
    {
        if ($this->request->isPost()) {
            // return json(['code' => 1, 'msg' => 'OK']); // 测试接口

            // 获取请求参数
            $department = $this->request->param('department');

            // 根据department 获取科室ID
            $this->model = new \app\admin\model\dicts\Department();
            if ($department == '全部科室') {
                $department_id = "all";  // 全部科室
            } else {
                $department_id = $this->model->where('department', $department)->value('id');
            }
            // 获取所有科室和ID 数组
            $dapartmentArr = $this->model->field('id,department')->select();

            $date_select = $this->request->param('date_select');
            //["start_time" => "2023-01-01 00:00:00", "start_timestamp" => 1609459200, "end_time" => "2023-01-31 23:59:59", "end_timestamp" => 1611137599]
            $dateStrartEnd = Utils::getMonthStartAndEnd($date_select);

            // 计算指定月份的开始时间和结束时间
            $start_time = $dateStrartEnd['start_timestamp'];
            $end_time = $dateStrartEnd['end_timestamp'];

            // 获取指定科室的指定月份的病区敏感数据
            $this->model = new \app\admin\model\fssy\Departmentmetrics();
            $data = $this->model
                ->where('updatetime', '>=', $start_time)
                ->where('updatetime', '<=', $end_time);
            if ($department_id !== "all") {
                $data = $data->where("department_id", '=', $department_id);
            }
            $data = $data->select();

            // 循环$data 数组 把时间戳转换成时间格式
            foreach ($data as $index => $item) {
                $item['createDate'] = date('Y-m-d', $item['createtime']);
                $item['updateDate'] = date('Y-m-d', $item['updatetime']);
                $item['createDay'] = date('d', $item['createtime']);
                $item['updateDay'] = date('d', $item['updatetime']);
                // 转换成Int格式
                $item['createDay'] = intval($item['createDay']);
                $item['updateDay'] = intval($item['updateDay']);
                // 循环$dapartmentArr 数组 找到科室
                foreach ($dapartmentArr as $i => $elem) {
                    if ($elem['id'] == $item['department_id']) {
                        $item['departmentName'] = $elem['department'];
                    }
                }
            }

            // $data = ['住院患者实际占用床日数' => array_fill(0, 30, 99)];
            return json(['code' => 1, 'msg' => 'OK', 'data' => $data]);
        }
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
}
